package com.stn.zjx.givingtreasure.activity.userwallet;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.came.viewbguilib.ButtonBgUi;
import com.google.gson.Gson;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lcodecore.tkrefreshlayout.header.progresslayout.ProgressLayout;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.stn.zjx.givingtreasure.R;
import com.stn.zjx.givingtreasure.adapter.DynamicShowItemListAdapter;
import com.stn.zjx.givingtreasure.adapter.UserTransferRecordItemListAdapter;
import com.stn.zjx.givingtreasure.base.BaseActivity;
import com.stn.zjx.givingtreasure.domain.UserAddressDomain;
import com.stn.zjx.givingtreasure.domain.UserTransferRecordDomain;
import com.stn.zjx.givingtreasure.event.NetWorkEvent;
import com.stn.zjx.givingtreasure.net.BaseApi;
import com.stn.zjx.givingtreasure.net.impl.PayNetImpl;
import com.stn.zjx.givingtreasure.net.impl.UserAddressNetImpl;
import com.stn.zjx.givingtreasure.service.UserService;
import com.stn.zjx.givingtreasure.table.User;
import com.stn.zjx.givingtreasure.widget.MoreLoading;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class UserTransferRecordListActivity extends BaseActivity {
    @BindView(R.id.back_bt)
    RelativeLayout backBt;
    @BindView(R.id.action_title)
    TextView actionTitle;
    @BindView(R.id.web_title)
    TextView webTitle;
    @BindView(R.id.action_add_bt)
    RelativeLayout actionAddBt;
    @BindView(R.id.action_refresh)
    RelativeLayout actionRefresh;
    @BindView(R.id.exit)
    RelativeLayout exit;
    @BindView(R.id.sure_bt)
    TextView sureBt;
    @BindView(R.id.skip_bt)
    TextView skipBt;
    @BindView(R.id.share_bt)
    RelativeLayout shareBt;
    @BindView(R.id.more_bt)
    RelativeLayout moreBt;
    @BindView(R.id.scan_bt)
    RelativeLayout scanBt;
    @BindView(R.id.release_bt)
    ButtonBgUi releaseBt;
    @BindView(R.id.search_iv)
    ImageView searchIv;
    @BindView(R.id.search_edit)
    EditText searchEdit;
    @BindView(R.id.search_bg)
    RelativeLayout searchBg;
    @BindView(R.id.save_bt)
    TextView saveBt;
    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.twinkling_refresh)
    TwinklingRefreshLayout twinklingRefresh;
    @BindView(R.id.no_data_tv)
    TextView noDataTv;
    UserTransferRecordItemListAdapter adapter;
    private LinearLayoutManager lm;
    private boolean isMoreLoading = false;
    private int visibleItemCount = 0;
    Integer pageNow = 1;
    Integer pageSize = 10;
    @Override
    public void networkStatus(NetWorkEvent netWorkEvent) {

    }

    @Override
    public void initComponent() {
          actionTitle.setText(getString(R.string.fine_balance));
        rv.setLayoutManager(lm = new LinearLayoutManager(this, OrientationHelper.VERTICAL, false));
        rv.setAdapter(adapter = new UserTransferRecordItemListAdapter(this));
        ProgressLayout headerView = new ProgressLayout(this);
        headerView.setColorSchemeResources(R.color.colorAccent);
        twinklingRefresh.setHeaderView(headerView);
        twinklingRefresh.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(final TwinklingRefreshLayout refreshLayout) {
                initRefresh();
            }

            @Override
            public void onLoadMore(final TwinklingRefreshLayout refreshLayout) {
//                if (!isMoreLoading &&
//                        adapter.dataBeanList.size() >= pageNow * 10) {

//                }else{
//                    twinklingRefresh.finishLoadmore();
//                }
            }
        });
        rv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (!isMoreLoading && newState == RecyclerView.SCROLL_STATE_IDLE && visibleItemCount == adapter.dataBeanList.size() - 1 &&
                        adapter.dataBeanList.size() >= pageNow * 10) {
                    //  initAppend(city, area, longitude, latitude);
                    initAppend();
                }
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                visibleItemCount = lm.findLastVisibleItemPosition();

            }
        });

        twinklingRefresh.startRefresh();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_transfer_record_list);
        ButterKnife.bind(this);
        initComponent();
    }

    @OnClick({R.id.back_bt})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_bt:
                finish();
                break;

        }
    }
    public void initRefresh() {
        User userOnline = UserService.getInstance(getApplicationContext()).getOnlineLoginUser();
        if (userOnline == null) {
            twinklingRefresh.finishRefreshing();
            return;
        }
        pageNow = 1;
        PayNetImpl
                .query_user_transfer_record(userOnline.getToken(),pageNow, pageSize, userOnline.getToken() + "_query_user_transfer_record",
                        new StringCallback() {
                            @Override
                            public void onFinish() {
                                super.onFinish();
                                twinklingRefresh.finishRefreshing();
                            }

                            @Override
                            public void onSuccess(Response<String> response) {
                                if (response.isSuccessful() && !isFinishing()) {
                                    UserTransferRecordDomain domain = new Gson().fromJson(response.body(), UserTransferRecordDomain.class);
                                    switch (domain.getStatus()) {
                                        case BaseApi.REQUEST_SUCCESS_CODE:
                                            if (domain.getData() != null && domain.getData().size() != 0) {
                                                noDataTv.setVisibility(View.GONE);
                                                adapter.refreshList(domain.getData());

                                            }else{
                                                noDataTv.setVisibility(View.VISIBLE);
                                                adapter.clearList();
                                            }
                                            break;
                                        default:
                                            noDataTv.setVisibility(View.VISIBLE);
                                            adapter.clearList();
                                            break;
                                    }
                                }
                            }

                            @Override
                            public void onCacheSuccess(Response<String> response) {
                                super.onCacheSuccess(response);
                                if (response.isSuccessful() && !isFinishing()) {
                                    UserTransferRecordDomain domain = new Gson().fromJson(response.body(), UserTransferRecordDomain.class);
                                    switch (domain.getStatus()) {
                                        case BaseApi.REQUEST_SUCCESS_CODE:
                                            if (domain.getData() != null && domain.getData().size() != 0) {
                                                noDataTv.setVisibility(View.GONE);
                                                adapter.refreshList(domain.getData());

                                            }else{
                                                noDataTv.setVisibility(View.VISIBLE);
                                                adapter.clearList();
                                            }
                                            break;
                                        default:
                                            noDataTv.setVisibility(View.VISIBLE);
                                            adapter.clearList();
                                            break;
                                    }
                                }
                            }
                        });
    }
    public void initAppend() {
        User userOnline = UserService.getInstance(getApplicationContext()).getOnlineLoginUser();
        if (userOnline == null) {
            isMoreLoading=false;
            return;
        }
        pageNow ++;
        PayNetImpl
                .query_user_transfer_record(userOnline.getToken(),pageNow, pageSize, userOnline.getToken() + "_query_user_transfer_record",
                        new StringCallback() {
                            @Override
                            public void onStart(Request<String, ? extends Request> request) {
                                super.onStart(request);
                                isMoreLoading=true;
                            }


                            @Override
                            public void onFinish() {
                                super.onFinish();
                                    isMoreLoading=false;
                            }

                            @Override
                            public void onSuccess(Response<String> response) {
                                if (response.isSuccessful() && !isFinishing()) {
                                    UserTransferRecordDomain domain = new Gson().fromJson(response.body(), UserTransferRecordDomain.class);
                                    switch (domain.getStatus()) {
                                        case BaseApi.REQUEST_SUCCESS_CODE:
                                            if (domain.getData() != null && domain.getData().size() != 0) {
                                                adapter.appendList(domain.getData());
                                            }
                                            break;

                                    }
                                }
                            }

                        });
    }
}
